#include <iostream>
#include <filesystem>

#include <mysql.h>

#include "config.h"
#include "db.h"
#include "db_op.h"
#include "db_info.h"
#include "lib_init.h"
#include "lib_manager.h"
#include "book_info.h"
#include "time.h"

int main() {

    // 读取数据库登录账户信息
    DatabaseInfo* db_info;
    auto value = read_database_info();
    if (!value.has_value()) {
#ifdef DEBUG_PRINTS
        std::cerr << "XML parsing failed!" << std::endl;
#endif
        return 1;
    }
    db_info = *value;


    // 连接数据库
    Library_Database* k = new Library_Database();
    if(!k->connect(db_info)){
        delete db_info;
        return 0;
    }
    delete db_info;




    // 建表 初始化
    LibraryTableCreator t = LibraryTableCreator(k);
    t.LibarayTableInit();

    // 加入采购信息

    Library_UpdateManager l = Library_UpdateManager(k);

    BookInfo a = BookInfo("S0001","bb","cc","dd",getTimeTFromYMD(2020,11,11),0);
    PurchaseInfo b = PurchaseInfo(-1,"aa",122,getTimeTFromYMD(2021,11,11),1.11);
    DiscardInfo c = DiscardInfo(-1,"aa",121,getTimeTFromYMD(2021,11,11));

    BorrowInfo d = BorrowInfo(-1,"aa","tt",getTimeTFromYMD(2021,11,11),getTimeTFromYMD(2022,11,11),"已借出");



    l.AddBookInfo(a);
    l.BookPurchase(b);
    l.BookElimination(c);
    l.BookRent(d);

    l.BookReturn("cc",getTimeTFromYMD(2022,1,11));



    // 查询

    Library_QueryManager q = Library_QueryManager(k);

    QueryResult r = q.GetBooksInfo();
    r.print();

    QueryResult r2 = q.GetPurchasesInfo();
    r2.print();

    QueryResult r3 = q.GetDiscardsInfo();
    r3.print();

    QueryResult r4 = q.GetBorrowsInfo();
    r3.print();


    // 统计信息
    Library_StatisticsManager s = Library_StatisticsManager(k);

    QueryResult r5 = s.GetBorrowsStatistics();
    r5.print();
    QueryResult r6 = s.GetDiscardsStatistics();
    r6.print();    
    QueryResult r7 = s.GetInventoryStatistics();
    r7.print();
    QueryResult r8 = s.GetPurchasesStatistics();
    r8.print();



    return 0;

}

